<style>
#DBRecordQueryTable{
	margin: 4px 0px;
}
#DBSyncButton{
	margin-left: 2px;
}
#DBQueryIdText{
	margin-right: 8px;
}
#DBQueryUsedText{
	margin-right: 8px;
}
#DBQueryRecordButton{
	margin-right: 24px;
}
#DatabaseEditTable td{
	padding: 6px;
}
</style>

<div>
	<table id='DBRecordQueryTable'>
		<tr>
			<td>
				<v-text id='DBQueryIdText' title='连接名称' maxlength='24'></v-text>
				<v-select id='DBQueryUsedText' title='数据用途' option='|停用|系统|用户'></v-select>
				<v-button id='DBQueryRecordButton' title='查询'></v-button>
				<v-button id='DBRecordAddButton' title='添加数据库'></v-button>
			</td>
		</tr>
	</table>
	<div id='DBRecordListDiv'></div>
</div>

<script>
getVue('DBRecordQueryTable');

var vmdata = {
	title: {
		id: centerText('连接名称', 'bold'),
		enabled: centerText('数据用途', 'bold'),
		type: centerText('连接类型', 'bold'),
		charset: centerText('字符编码', 'bold'),
		name: '数据库名',
		host: centerText('连接地址', 'bold'),
		port: centerText('监听端口', 'bold'),
		user: centerText('连接用户', 'bold')
	},
	check: function(data){
		if (data.route) this.button[2] = {title: '发布',click: publishDBRecord};
	},
	filter: {
		id: centerText,
		host: centerText,
		port: centerText,
		user: centerText,
		type: function(val){
			return centerText(val.toUpperCase());
		},
		enabled: function(val){
			var list = ['停用', '系统', '用户'];
			return centerText(list[val]);
		},
		charset: function(val){
			return centerText(val ? val.toUpperCase() : '默认');
		},
	},
	button: [{
			title: '编辑',
			click: editDBRecord
		},{
			title: '删除',
			click: removeDBRecord
		}
	]
};

var listview = new RecordListView('DBRecordListDiv', '/GetDatabaseList', vmdata, function(){
	return {id: $('#DBQueryIdText').val(), enabled: $('#DBQueryUsedText').val() - 1};
});

$('#DBRecordAddButton').click(function(){
	editDBRecord();
});

$('#DBQueryIdText').keyup(function(){
	listview.reload(false);
});

$('#DBQueryUsedText').change(function(){
	listview.reload(false);
});

$('#DBQueryRecordButton').click(function(){
	listview.reload(true);
});

function checkHost(host){
    var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
    return reg.test(host);
}

function editDBRecord(item){
	var title = item ? '更新连接' : '添加连接';
	var msgbox = showConfirmMessage("<table id='DatabaseEditTable'>"
		+ "<tr><td><v-select id='DBUsedText' title='数据用途' option='停用|系统|用户'></v-select></td></tr>"
		+ "<tr><td><v-select id='DBCharText' title='字符编码' option='默认|GBK|UTF'></v-select></td></tr>"
		+ "<tr><td><v-select id='DBTypeText' title='连接类型' option='MYSQL|SQLITE'></v-select></td></tr>"
		+ "<tr><td><v-text id='DBIdText' title='连接名称' maxlength='32' size='13'></v-text><v-button id='DBSyncButton' title='同步配置'></v-button></td></tr>"
		+ "<tr><td><v-text id='DBNameText' title='数据库名' maxlength='128' size='24'></v-text></td></tr>"
		+ "<tr><td><v-text id='DBHostText' title='连接地址' maxlength='32' size='24'></v-text></td></tr>"
		+ "<tr><td><v-text id='DBPortText' title='监听端口' maxlength='5' size='24'></v-text></td></tr>"
		+ "<tr><td><v-text id='DBUserText' title='连接用户' maxlength='32' size='24'></v-text></td></tr>"
		+ "<tr><td><v-text id='DBPasswdText' title='连接密码' type='password' maxlength='32' size='24'></v-text></td><td><img src='/res/img/eyeclose.png'/></td></tr>",
		title, function(flag){
		if (flag == 0) return true;

		var id = $('#DBIdText').val();
		var type = $('#DBTypeText').val();
		var name = $('#DBNameText').val();
		var host = $('#DBHostText').val();
		var port = $('#DBPortText').val();
		var user = $('#DBUserText').val();
		var passwd = $('#DBPasswdText').val();
		var enabled = $('#DBUsedText').val();
		var charset = $('#DBCharText').val();

		if (strlen(id) == 0){
			$('#DBIdText').focus();
			return false;
		}

		if (strlen(name) == 0){
			$('#DBNameText').focus();
			return false;
		}

		var param = {};
		var typelist = ['mysql', 'sqlite'];
		var charlist = ['', 'GBK', 'UTF-8'];

		type = typelist[type];
		charset = charlist[charset];

		if (item && charset == item.charset
			&& id == item.id && type == item.type
			&& name == item.name && host == item.host
			&& port == item.port && user == item.user
			&& passwd == item.passwd && enabled == item.enabled){
			return true;
		}
		
		if (item == null){
			var found = false;

			$('.DBIdTd').each(function(){
				if (id == $(this).text()) found = true;
			});

			if (found){
				msgbox.title('连接名称重复');

				$('#DBIdText').focus();

				setTimeout(function(){
					msgbox.title(title);
				}, 1000);

				return false;
			}
		}

		param['id'] = id;
		param['type'] = type;
		param['name'] = name;
		param['enabled'] = enabled;
		param['charset'] = charset;

		if (type == 'mysql'){
			if (strlen(host) == 0){
				$('#DBHostText').focus();
				return false;
			}

			if (!checkHost(host)){
				$('#DBHostText').val('').focus();
				return false;
			}

			if (strlen(port) == 0){
				$('#DBPortText').focus();
				return false;
			}
			
			if (strlen(user) == 0){
				$('#DBUserText').focus();
				return false;
			}
			
			if (strlen(passwd) == 0){
				$('#DBPasswdText').focus();
				return false;
			}
			
			param['host'] = host;
			param['port'] = port;
			param['user'] = user;
			param['passwd'] = passwd;
		}

		param['flag'] = item ? 'U' : 'A';
		param['step'] = '1';
		param['stress'] = '0';

		getHttpResult('/EditDatabase', param, function(data){
			if (data.code == XG_TIMEOUT){
				sessionTimeout();
			}
			else if (data.code < 0){
				showToast(title + '失败');
				flag = false;
			}
			else{
				showToast(title + '成功');
				
				if (data.code > 0){
					listview.reload(false);
				}
			}
		});
		
		return flag;
	});
	
	function updateParam(item, sync){
		if (item == null){
			$('#DBTypeText').change();
			return false;
		}

		$('#DBIdText').val(item.id);
		$('#DBNameText').val(item.name);
		$('#DBHostText').val(item.host);
		$('#DBPortText').val(item.port);
		$('#DBUserText').val(item.user);
		$('#DBUsedText').val(item.enabled);
		$('#DBPasswdText').val(item.passwd);

		$('#DBTypeText').val(0);
		$('#DBCharText').val(0);

		if (item.type == 'sqlite') $('#DBTypeText').val(1);
		if (item.charset == "GBK") $('#DBCharText').val(1);
		if (item.charset == "UTF-8") $('#DBCharText').val(2);

		if (sync == null){
			$('#DBIdText').attr('disabled', true);
		}

		$('#DBTypeText').change();

		return true;
	}

	$('#DBPasswdText').parent().parent().next().children('img').width('16px').height('16px').css('margin-top', '4px').css('margin-left', '3px').click(function(){
		if (this.src.indexOf('close') > 0){
			this.src = '/res/img/eyeopen.png';
			$('#DBPasswdText').attr('type', 'text');
		}
		else{
			this.src = '/res/img/eyeclose.png';
			$('#DBPasswdText').attr('type', 'password');
		}
	});

	$('#DBSyncButton').click(function(){
		var id = $('#DBIdText').val();

		if (strlen(id) == 0){
			$('#DBIdText').focus();
			return false;
		}

		msgbox.title('正在同步配置...');

		getHttpResult('/ExecModule', {cmd: 'get', type: 'database', name: id}, function(data){
			if (data.code < 0){
				msgbox.title('配置信息同步失败');
			}
			else{
				var found = null;

				$.each(data.list, function(idx, item){
					if (id == item.id){
						updateParam(item, true);
						found = item;
						return true;
					}
				});

				if (found == null){
					msgbox.title('没有相关配置信息');
				}
				else{
					msgbox.title('配置信息已同步');
				}
			}

			setTimeout(function(){
				msgbox.title(title);
			}, 2000);
		});
	});

	$('#DBTypeText').change(function(){
		if ($(this).val() == 0){
			$('#DBHostText').removeAttr('disabled').prev().css('color', '#000');
			$('#DBPortText').removeAttr('disabled').prev().css('color', '#000');
			$('#DBUserText').removeAttr('disabled').prev().css('color', '#000');
			$('#DBPasswdText').removeAttr('disabled').prev().css('color', '#000');
		}
		else{
			$('#DBHostText').attr('disabled', true).prev().css('color', '#888');
			$('#DBPortText').attr('disabled', true).prev().css('color', '#888');
			$('#DBUserText').attr('disabled', true).prev().css('color', '#888');
			$('#DBPasswdText').attr('disabled', true).prev().css('color', '#888');
		}
	});

	$('#DBPortText').keyup(function(){
		this.value = this.value.replace(/[^\0-9]/g, '');
	}).change(function(){
		this.value = this.value.replace(/[^\0-9]/g, '');
	});

	updateParam(item);
}

function removeDBRecord(item){
	showConfirmMessage('是否决定删除[' + item.id + ']连接？', '删除连接', function(flag){
		if (flag){
			getHttpResult('/EditDatabase', {flag: 'D', id: item.id}, function(data){
				if (data.code == XG_TIMEOUT){
					sessionTimeout();
				}
				else if (data.code < 0){
					showToast('删除数据库连接失败');
				}
				else{
					showToast('删除数据库连接成功');
					listview.remove(item);
				}
			});
		}
	});
}

function publishDBRecord(item){
	showConfirmMessage('是否决定发布[' + item.id + ']配置？', '发布配置', function(flag){
		if (flag){
			var param = 'cmd=sync&type=database&name=' + item.id;

			getHttpResult('/Broadcast', {path: 'ExecModule', param: param}, function(data){
				if (data.code == XG_TIMEOUT){
					sessionTimeout();
				}
				else if (data.code < 0){
					showToast('发布配置失败');
				}
				else{
					showToast('发布配置成功');
				}
			});
		}
	});
}
</script>